package com.music.filter;

import com.music.pojo.UserInfo;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "VisitorFilter",urlPatterns = {"*.do"})
public class VisitorFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        UserInfo userInfo = (UserInfo) req.getSession().getAttribute("user");
        
        if (userInfo == null){
            resp.sendRedirect(req.getContextPath()+"/page/login.html");
        }else {
            chain.doFilter(request, response);
        }
    }
}
